﻿using System;
using System.Linq;
using System.Web.Mvc;
using Abmemunit.Web.Models;
using Abmemunit.Services.Users;
using Abmemunit.Domain.Users;

namespace Abmemunit.Web.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            this._userService = userService;
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LogOn()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            if (ModelState.IsValid)
            {

                model.UserName = model.UserName.Trim();
                var user =
                    new User()
                        {
                            Username = model.UserName,
                            Password = model.Password
                        };
                if (_userService.ValidateUser(user))
                {
                    var dbUser = _userService.GetUserByName(model.UserName);

                    if (dbUser.Active == false)
                    {
                        ModelState.AddModelError("","Your account is not active now");
                        return View();
                    }
                    
                    _userService.SignIn(this.HttpContext, dbUser, model.RememberMe);
                    return RedirectToAction("Index", "Home");

                }
                else
                {
                    ModelState.AddModelError("","We didn't recognize the user name or password you entered.");
                    return View();
                }
            }

            return View("index");
        }

        public ActionResult Register()
        {
            var model = new RegisterModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {

                var user =
                    new User()
                        {
                            UserGuid = Guid.NewGuid(),
                            Email = model.Email,
                            Username = model.UserName,
                            Password = model.Password,
                            PasswordFormat = PasswordFormat.Hashed,
                            PasswordSalt = "",
                            Active = model.Active,
                            IsAdminAccount = model.IsAdminAccount,
                            CreatedOnUtc = DateTime.UtcNow,
                            MailingAddress = model.MailingAddress

                        };

                _userService.CreateUser(user);

                return Content("Member creation success");
            }
            else
            {
                return Content("Invalid input, please correct it.");
            }

        }

        public ActionResult LogOut()
        {
            _userService.SignOut();
            return RedirectToAction("Index", "Home");
        }
        public ActionResult Profile()
        {
            var curUser = new UserViewModel();

            if (!string.IsNullOrEmpty(this.HttpContext.User.Identity.Name))
            {
                var dbUser = _userService.GetUserByName(this.HttpContext.User.Identity.Name);
                curUser = new UserViewModel()
                {
                    Id = dbUser.Id,
                    Username = dbUser.Username,
                    Email = dbUser.Email,
                    Password = dbUser.Password,
                    IsAdminAccount = dbUser.IsAdminAccount,
                    MailingAddress = dbUser.MailingAddress,
                    LastLoginDateUtc = dbUser.LastLoginDateUtc,
                    CreatedOnUtc = dbUser.CreatedOnUtc

                };


            }


            return View(curUser);
        }
        [HttpPost]
        public ActionResult Profile(UserViewModel model)
        {

            if (ModelState.IsValid)
            {
                var dbUser = _userService.GetUserById(model.Id);
                if (dbUser != null)
                {
                    dbUser.Email = model.Email;
                    dbUser.MailingAddress = model.MailingAddress;
                    _userService.UpdateUser(dbUser);


                    return Content("Profile information successfully updated");
                }

            }
            return Content("Sorry! unable to update profile");
        }

        
        public ActionResult MenuView()
        {

            bool isAdminUser = false;

            if (!string.IsNullOrEmpty(this.HttpContext.User.Identity.Name))
            {
                var dbUser = _userService.GetUserByName(this.HttpContext.User.Identity.Name);

                if (dbUser != null && dbUser.IsAdminAccount==true)
                {
                    isAdminUser = true;
                }
            }

            return PartialView("_menuView", isAdminUser);
        }

        public ActionResult UserList()
        {
            var users = _userService.GetUsers().ToList()
                .Select(x =>
                        new UserViewModel
                            {
                                Id = x.Id,
                                Username = x.Username,
                                Email = x.Email,
                                Password = x.Password,
                                IsAdminAccount = x.IsAdminAccount,
                                MailingAddress = x.MailingAddress,
                                LastLoginDateUtc = x.LastLoginDateUtc,
                                CreatedOnUtc = x.CreatedOnUtc,
                                Active = x.Active
                                
                            });
            return View(users);
        }

        public ActionResult Edit(int id)
        {
            var delUser = _userService.GetUserById(id);
            var model =
                new UserViewModel()
                    {
                        Id = delUser.Id,
                        Username = delUser.Username,
                        Email = delUser.Email,
                        Password = delUser.Password,
                        IsAdminAccount = delUser.IsAdminAccount,
                        MailingAddress = delUser.MailingAddress,
                        LastLoginDateUtc = delUser.LastLoginDateUtc,
                        CreatedOnUtc = delUser.CreatedOnUtc,
                        Active = delUser.Active
                        
                    };


            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var dbUser = _userService.GetUserById(model.Id);
                if (dbUser!=null)
                {
                    dbUser.IsAdminAccount = model.IsAdminAccount;
                    dbUser.Active = model.Active;
                    dbUser.MailingAddress = model.MailingAddress;
                    _userService.UpdateUser(dbUser);
                    

                    return Content("Member information successfully updated");
                }

            }
            return Content("Sorry! unable to update Member information");
        }
        
        public ActionResult Delete(int id)
        {
            var delUser = _userService.GetUserById(id);
            
            _userService.DeleteUser(id);

            var model = new UserViewModel()
            {
                Id = delUser.Id,
                Username = delUser.Username,
                Email = delUser.Email,
                Password = delUser.Password,
                IsAdminAccount = delUser.IsAdminAccount,
                MailingAddress = delUser.MailingAddress,
                LastLoginDateUtc = delUser.LastLoginDateUtc,
                CreatedOnUtc = delUser.CreatedOnUtc,
                Active = delUser.Active
                
            };
            return View(model);
        }


    }
}
